//
//  DeterrenceVC.m
//  Yihubang
//
//  Created by xmy on 2016/11/11.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "DeterrenceVC.h"
#import "DeterrenceGuideView.h"
#import "DeterenceBackAnimationView.h"
#import "MyMessageCenterVC.h"
#import "PopoverView.h" //点击弹出框

@interface DeterrenceVC ()<DeterrenceGuideViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *DeterToSetScene;
@property (weak, nonatomic) IBOutlet UIImageView *arrowAnimation_img;
@property (strong, nonatomic) DeterrenceGuideView *deterrenceGuideView;
@property (nonatomic , strong) NSMutableArray *datasource ;

@end

@implementation DeterrenceVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addGlobalView];
    [self.DeterToSetScene addTarget:self action:@selector(pushToScene) forControlEvents:UIControlEventTouchUpInside];
    [self setArrowAnimation];
    
    
    
}

#pragma mark - 添加悬浮通知按钮

//添加按钮
-(void)addGlobalView
{
    KGlobalView *_globalView = [KGlobalView sharedKGlobalView];
    _globalView.frame = CGRectMake(ScreenWidth - RatioBase5(67), ScreenHeight/2 - 22 , RatioBase5(67), RatioBase5(67));
    _globalView.imageView.image = [UIImage imageNamed:@"通知"];
    [_globalView setBackgroundColor:[UIColor clearColor]];
    
    @weakify(self)
    _globalView.ClickDragViewBlock = ^(WMDragView *dragView){
        @strongify(self)
        [self showAnotherView];//显示弹出框
        [KGlobalView sharedKGlobalView].imageView.image = [UIImage imageNamed:@"通知"];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:_globalView];
}

-(void)showAnotherView
{
    [KGlobalView sharedKGlobalView].hidden = YES;
    PopoverView *popoverView = [PopoverView popoverView];
    popoverView.showMarkParams = [KGlobalView sharedKGlobalView].showMarkParams;
    [popoverView showToView:[KGlobalView sharedKGlobalView] withActions:[self QQActions]];
}


- (NSArray<PopoverAction *> *)QQActions {
    PopoverAction *myHelpAction = [PopoverAction actionWithImage:[UIImage imageNamed:@"我的"] title:@"我的求助" handler:^(PopoverAction *action) {
        [self pushViewWithName:@"我的求助"];
    }];
    PopoverAction *heHelpAction = [PopoverAction actionWithImage:[UIImage imageNamed:@"他的"] title:@"他的求助" handler:^(PopoverAction *action) {
        [self pushViewWithName:@"他的求助"];
    }];
    return @[myHelpAction, heHelpAction];
}

// 跳转界面
- (void)pushViewWithName:(NSString *)name {
    UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    BaseViewController * BaseViewController = [HelpStory instantiateViewControllerWithIdentifier:@"MyMessageCenterVC"];
    BaseViewController.params =@{@"title":name};

    UIViewController *currentViewController =  [MasterUrlManager shareMasterUrlManager].currentVC;
    if ([[NSString stringWithFormat:@"%@",currentViewController.class] isEqualToString:@"MyMessageCenterVC"]) {
        currentViewController.params = @{@"title":name};
    }else{
    [SharedAppDelegate.window.rootViewController pushViewController:BaseViewController animated:YES];
    }
    

}

- (void)showRotationView{
    [KGlobalView sharedKGlobalView].hidden = NO;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
    NSString* DeterrenceFirst = [userDefault objectForKey:DeterrenceFirstKey];
    if (DeterrenceFirst == nil) {
        [KGlobalView sharedKGlobalView].hidden = YES;
        [self addGuideView];
        [userDefault setBool:YES forKey:DeterrenceFirstKey];
        [userDefault synchronize];
    }
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setArrowAnimation{
//    [self.arrowAnimation_img setImage:[UIImage imageNamed:@"arrowImg-1"]];
    
    self.arrowAnimation_img.animationImages = @[[UIImage imageNamed:@"arrowImg-1"],
                                            [UIImage imageNamed:@"arrowImg-2"],
                                            [UIImage imageNamed:@"arrowImg-3"],
                                            [UIImage imageNamed:@"arrowImg-2"]];
    self.arrowAnimation_img.animationDuration = 0.8f;
    self.arrowAnimation_img.animationRepeatCount = 0;
    [self.arrowAnimation_img startAnimating];
}

-(void)addGuideView{
    self.deterrenceGuideView = [[UINib nibWithNibName:@"DeterrenceGuideView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
    self.deterrenceGuideView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.deterrenceGuideView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:self.deterrenceGuideView];
}


-(void)overAction{
    [self.delegate deterrenceVCBeginGuideAction];
}


-(void)pushToScene{
    UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"GuardMe" bundle:[NSBundle mainBundle]];
    BaseViewController * BaseViewController = [HelpStory instantiateViewControllerWithIdentifier:@"SceneSettingViewController"];
    [self.navigationController pushViewController:BaseViewController animated:YES];
}
//

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
